package com.acxiom.bsh.controller.interceptor;

import java.net.URLEncoder;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.acxiom.bsh.constants.ThreadLocalContextHolder;
import com.acxiom.bsh.logger.LoggerManager;
import com.acxiom.bsh.pojo.GlobalContext;
import com.google.gson.Gson;

@Aspect
@Component
public class ControllerInterceptor {

	@Pointcut("execution(* com.acxiom.bsh.controller..*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public void controllerMethodPointcut() {
	}

	@Around("controllerMethodPointcut()")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes sra = (ServletRequestAttributes) ra;
		HttpServletRequest request = sra.getRequest();

		String source = request.getHeader("sellerSource");
		ThreadLocalContextHolder.put(new GlobalContext());
		if (StringUtils.isNotBlank(source)) {
			String sellerName=null;
			if("1".equals(source)) {
				sellerName="西门子家电官方旗舰店";
			}else if("2".equals(source)){
				sellerName="西门子家电厨卫旗舰店";
			}else if("3".equals(source)){
				sellerName="博西华电器专营店";
			}
			
			ThreadLocalContextHolder.get().setValue(URLEncoder.encode(sellerName, "UTF-8"));
		}

		String url = request.getRequestURL().toString();
		String method = request.getMethod();
		String uri = request.getRequestURI();
		Object[] arguments = pjp.getArgs();
		String param = getMethodArgs(arguments);
		String controllerClass = pjp.getTarget().getClass().getName();
		String controllerMethod = pjp.getSignature().getName();
		String requestId = null;

		if (requestId == null) {
			requestId = UUID.randomUUID().toString();
		}

		// print request param
		LoggerManager.getApiLogger().info("Request Start. requestId: {}, url: {}, uri: {}, request method: {}, controller class: {}, controller method: {}, request params: {},sellerSource:{}", requestId, url, uri, method, controllerClass, controllerMethod, param,source);

		// result的值就是被拦截方法的返回值
		Object result = pjp.proceed();
		Gson gson = new Gson();

		// print response param
		LoggerManager.getApiLogger().info("Request End. requestId: {}, controller class: {}, controller method: {}, response params: {}", requestId, controllerClass, controllerMethod, gson.toJson(result));
		return result;
	}

	private String getMethodArgs(Object[] args) {
		StringBuffer sb = new StringBuffer();
		Gson gson;
		for (int i = 0; i < args.length; i++) {
			gson = new Gson();
			sb.append("arg[" + i + "]: " + gson.toJson(args[i]) + ",");
		}
		if (args.length > 0) {
			sb.deleteCharAt(sb.length() - 1);
		}
		return sb.toString();
	}

}